package com.wash.shoes.controller.pc;


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.wash.shoes.domain.dto.SendUserCouponDto;
import com.wash.shoes.domain.vo.CustomerCouponVo;
import com.wash.shoes.entity.CouponPo;
import com.wash.shoes.entity.CustomerCouponPo;
import com.wash.shoes.entity.CustomerPo;
import com.wash.shoes.exception.WashException;
import com.wash.shoes.service.CouponService;
import com.wash.shoes.service.CustomerCouponService;
import com.wash.shoes.service.CustomerService;
import com.wash.shoes.util.WsRandomUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ljg
 * @since 2024-12-01
 */
@Slf4j
@Api(tags = {"PC-用户的优惠卷"})
@RestController
@RequestMapping({"/customerCoupon", "/dev-api/customerCoupon"})
public class CustomerCouponController {

    @Autowired
    CustomerCouponService customerCouponService;

    @Autowired
    CouponService couponService;

    @Autowired
    CustomerService customerService;

    @ApiOperation("获取优惠卷列表")
    @PostMapping("/list")
    public R<Page<CustomerCouponVo>> pageList(@RequestBody CustomerCouponVo customerCouponVo) {
        Page<CustomerCouponVo> page = customerCouponService.pageList(customerCouponVo);
        return R.ok(page);
    }

    @ApiOperation("修改新增,有id为修改没有为新增")
    @PostMapping("/saveUpdate")
    public R<CustomerCouponPo> add(@RequestBody SendUserCouponDto sendUserCouponDto) {
        // 客户
        log.info("SendUserCouponDto {}", JSONObject.toJSON(sendUserCouponDto));
        CustomerPo customerPo = customerService.getById(sendUserCouponDto.getCustomerId());
        if (ObjectUtil.isNull(customerPo)) {
            customerPo = customerService.getByPhone(sendUserCouponDto.getCustomerId());
        }
        if (ObjectUtil.isNull(customerPo)) {
            throw new WashException("用户不存在");
        }
        // 优惠卷
        CouponPo couponPo = couponService.getById(sendUserCouponDto.getCouponId());

        // 优惠卷的领取+1
        couponPo.setReceiveCount(couponPo.getReceiveCount() + 1);
        couponService.updateById(couponPo);

        // 添加优惠卷
        CustomerCouponPo add = new CustomerCouponPo();
        add.setCustomerId(customerPo.getId());
        add.setCouponId(sendUserCouponDto.getCouponId());
        add.setCouponNo("WS" + WsRandomUtil.generateByRandom(10));
        add.setName(couponPo.getName());
        add.setAmount(couponPo.getAmount());
        add.setFullMoney(couponPo.getFullMoney());
        customerCouponService.save(add);
        return R.ok(add);
    }

    @ApiOperation("逻辑删除修改 status为0")
    @PostMapping("/deleteByStatus")
    public R<CustomerCouponPo> deleteByStatus(String theId) {

        CustomerCouponPo customerCouponPo = customerCouponService.getById(theId);
        customerCouponService.removeById(customerCouponPo);

        // 删除领取
        CouponPo couponPo = couponService.getById(customerCouponPo.getCouponId());
        couponPo.setReceiveCount(couponPo.getReceiveCount() - 1);
        couponService.updateById(couponPo);

        return R.ok(customerCouponPo);
    }

    @ApiOperation("获取详情")
    @PostMapping("/getDetailById")
    public R<CustomerCouponPo> getDetailById(String theId) {
        CustomerCouponPo customerCouponPo = customerCouponService.getById(theId);
        return R.ok(customerCouponPo);
    }

}

